-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correctly resolve tags for function overloads #30253
Conversation
There's a common thing people do where they only add JSDoc to the first overload. My feeling is that we should first try to get JSDoc for the applicable overload, and if we don't find anything, we should try to find JSDoc for any other overload. Thoughts @mjbvz @RyanCavanaugh? |
I typically don't add jsdoc comments, but when I do, I use it to deprecate a single overload. |
@ajafff please post future comments in the form of a captioned image meme if possible. |
@DanielRosenwasser That's a good point. I'm going to look at Definitely Typed. So far, it looks like the numbers are really low compared to compared to the total number of overloads. Admittedly, I'm only looking at d.ts and DT is relatively well-documented. Some super early numbers: I'll run over all of DT (and its dependencies) but right now I think this is a good change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One suggestion, plus a request for more tests.
@@ -501,7 +501,7 @@ namespace ts.SymbolDisplay { | |||
|
|||
if (!documentation) { | |||
documentation = symbol.getDocumentationComment(typeChecker); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should add the same code here.
/////** | ||
//// * @tag Tag text | ||
//// */ | ||
////declare function /*2*/foo(x: number): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test is a good start.
- can you add tests for documentation as well?
- can you add a test with the sequences [doc/no-tag, doc/tag], [doc/no-tag, doc/no-tag], [no-doc/no-tag, doc/tag], [no-doc/no-tag, doc/no-tag], [no-doc/no-tag, no-doc/tag]? There are other combinations but those are the ones that come to mind.
- I couldn't get this to work in my editor when I played with it locally, although the tests pass. Probably I just set it up wrong, but you might want to make sure it at leasts works in your editor.
Actually, looks like I was asking the wrong question. Turns out the number of first-jsdoc-only overloads is quite high, of the overloads with jsdoc: The yellow line is overloads with any jsdoc; the red line is overloads with jsdoc only on the first signature. Blue is all overloads. X=size of overload set; Y=number of each size of set. Looks like @DanielRosenwasser is right; the jsdoc for the first signature of the overload should be applied to all others that do not themselves have jsdoc. The same is not true for non-first signatures. |
@sandersn Pushed a better fix and added more unit tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good. The tests are subtly wrong in a couple of places. Can you investigate? If you no longer have time, let me know and I'll take a look.
{ name: "f3", text: "function f3(a: number): number (+1 overload)" }, | ||
{ name: "f4", text: "function f4(a: number): number (+1 overload)", documentation: "this is signature 4 - with number parameter" }, | ||
], | ||
}, | ||
{ | ||
marker: "18", | ||
includes: [ | ||
{ name: "i1_i", text: "var i1_i: i1\nnew (b: number) => any (+1 overload)" }, | ||
{ name: "i1_i", text: "var i1_i: i1\nnew (b: number) => any (+1 overload)", documentation: "new 1" }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this documentation is supposed to be on new (a: string)
, not new (b: number)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took a look at this and it's working as intended following:
the jsdoc for the first signature of the overload should be applied to all others that do not themselves have jsdoc
Is this an exception to the above rule?
Should this exception only be applied when useConstructSignatures === true
?
TypeScript/src/services/symbolDisplay.ts
Lines 222 to 226 in 376acad
if (useConstructSignatures) { | |
displayParts.push(keywordPart(SyntaxKind.NewKeyword)); | |
displayParts.push(spacePart()); | |
} | |
addSignatureDisplayParts(signature, allSignatures, TypeFormatFlags.WriteArrowStyleSignature); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I just missed that rule.
@@ -363,7 +363,7 @@ verify.signatureHelp({ marker: "38", overloadsCount: 2, docComment: "this is sig | |||
verify.quickInfoAt("38q", "var i3_i: i3\n(a: number) => number (+1 overload)", "this is signature 1"); | |||
|
|||
verify.signatureHelp({ marker: "39", overloadsCount: 2 }); | |||
verify.quickInfoAt("39q", "var i3_i: i3\n(b: string) => number (+1 overload)"); | |||
verify.quickInfoAt("39q", "var i3_i: i3\n(b: string) => number (+1 overload)", "this is signature 1"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same: this documentation is supposed to be on (a: number)
not (b: string)
.
@@ -428,7 +428,7 @@ verify.signatureHelp({ marker: "57", overloadsCount: 2, docComment: "c2 1" }); | |||
verify.quickInfoAt("57q", "constructor c2(a: number): c2 (+1 overload)", "c2 1"); | |||
|
|||
verify.signatureHelp({ marker: "58", overloadsCount: 2 }); | |||
verify.quickInfoAt("58q", "constructor c2(b: string): c2 (+1 overload)"); | |||
verify.quickInfoAt("58q", "constructor c2(b: string): c2 (+1 overload)", "c2 1"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same: (a: number)
is the one with jsdoc, not (b: string)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the confusion, I didn't re-read the whole thread in enough detail.
* Correctly resolve tags for function overloads. Fixes microsoft#30181 * Better fix for microsoft#30181. Added more unit tests * Fix commentsOverloads tests * Fallback to first signature when doc and tags are empty
Fixes #30181 #35933